package com.qict.xinba;

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 
 * @author tanwenwei
 * 
 */
public class MainActivity extends FragmentActivity {

	private FrameLayout videoview;
	private WebView videowebview;
	private Boolean islandport = true;
	private View xCustomView;
	private xWebChromeClient xwebchromeclient;
	private String root = "http://wap.bxlgou.com";
	private String url = root+"/";
	private WebChromeClient.CustomViewCallback 	xCustomViewCallback;
	private String id;
	private String outTradeNo;
	private IWXAPI api;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		api = WXAPIFactory.createWXAPI(this, "wxcd391c2ab8b96890",false);
		api.registerApp("wxcd391c2ab8b96890");
		initwidget();
		initListener();
		videowebview.loadUrl(url);
	}

	private void initListener() {

	}

	private void initwidget() {
		// TODO Auto-generated method stub
		videoview = (FrameLayout) findViewById(R.id.video_view);
		videowebview = (WebView) findViewById(R.id.video_webview);
		WebSettings ws = videowebview.getSettings();

		ws.setUserAgentString(ws.getUserAgentString()+"/XINBA/1");
		ws.setBuiltInZoomControls(true);
		ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		ws.setUseWideViewPort(true);
		ws.setLoadWithOverviewMode(true);
		ws.setSavePassword(true);
		ws.setSaveFormData(true);
		ws.setJavaScriptEnabled(true);
		ws.setDomStorageEnabled(true);
		ws.setGeolocationEnabled(true);
		xwebchromeclient = new xWebChromeClient();
		videowebview.setWebChromeClient(xwebchromeclient);
		videowebview.setWebViewClient(new xWebViewClientent());
	}

	class Listener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {

			default:
				break;
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && videowebview.canGoBack()) {
			videowebview.goBack();// 返回前一个页面
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	    public boolean inCustomView() {
	 		return (xCustomView != null);
	 	}

	     public void hideCustomView() {
	    	 xwebchromeclient.onHideCustomView();
	 	}

	public class xWebChromeClient extends WebChromeClient {
		private Bitmap xdefaltvideo;
		private View xprogressvideo;

		@Override

		public void onShowCustomView(View view, CustomViewCallback callback) {
			if (islandport) {

			} else {

			}
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			videowebview.setVisibility(View.GONE);
			if (xCustomView != null) {
				callback.onCustomViewHidden();
				return;
			}

			videoview.addView(view);
			xCustomView = view;
			xCustomViewCallback = callback;
			videoview.setVisibility(View.VISIBLE);
		}

		@Override
		public void onHideCustomView() {

			if (xCustomView == null)
				return;

			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			xCustomView.setVisibility(View.GONE);

			videoview.removeView(xCustomView);
			xCustomView = null;
			videoview.setVisibility(View.GONE);
			xCustomViewCallback.onCustomViewHidden();

			videowebview.setVisibility(View.VISIBLE);

		}

		@Override
		public Bitmap getDefaultVideoPoster() {
			if (xdefaltvideo == null) {
				xdefaltvideo = BitmapFactory.decodeResource(
						getResources(), R.drawable.videoicon);
			}
			return xdefaltvideo;
		}

		@Override
		public View getVideoLoadingProgressView() {

			if (xprogressvideo == null) {
				LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
				xprogressvideo = inflater.inflate(R.layout.video_loading_progress, null);
			}
			return xprogressvideo;
		}
		@Override
		public void onReceivedTitle(WebView view, String title) {
			(MainActivity.this).setTitle(title);


		}

		public void onGeolocationPermissionsShowPrompt(String origin,
													   GeolocationPermissions.Callback callback) {
			callback.invoke(origin, true, false);
			super.onGeolocationPermissionsShowPrompt(origin, callback);
		}

	}


	public class xWebViewClientent extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			Log.i("webviewtest", "shouldOverrideUrlLoading: " + url);
			return false;
		}

	}



}
